package mygui

import (
	//"image/color"
	"fyne.io/fyne/v2"
	//"fyne.io/fyne/v2/app"
	//"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
	//"os"
	"fmt"
	//"github.com/flopp/go-findfont"
	//"strings"
	//"reflect"
	"strconv"
)

/*
func init() {
    fontPaths := findfont.List()
    for _, path := range fontPaths {
        //fmt.Println(path)
        //楷体:simkai.ttf
        //黑体:simhei.ttf
        if strings.Contains(path, "simkai.ttf") {
            fmt.Println(path)
            os.Setenv("FYNE_FONT", path) // 设置环境变量  // 取消环境变量 os.Unsetenv("FYNE_FONT")
            break
        }
    }
    fmt.Println("=============")
}
*/


func Modify_main(myApp fyne.App, t_data chan int) fyne.Window {
	//var sadata = []string{"0","0"}
	var ladata = []string{"游戏Fan", "联想", "九游","一元"}
	var vadata = []string{"新增","消费"}
	//myApp := app.New()
	myWindow := myApp.NewWindow("修改界面")


	content1 := widget.NewLabel("修改前")
	input1 := widget.NewEntry()
	input1.SetPlaceHolder("游戏名字...")
	input1.Resize(fyne.NewSize(100, 30))
	lalist1 := widget.NewSelect(ladata, func(value string) {
		//fmt.Println("Select set to", value)
	})
	valist1 := widget.NewSelect(vadata, func(value string) {
		//fmt.Println("Select set to", value)
	})

	content2 := widget.NewLabel("修改后")
	input2 := widget.NewEntry()
	input2.SetPlaceHolder("游戏名字...")
	lalist2 := widget.NewSelect(ladata, func(value string) {
		//fmt.Println("Select set to", value)
	})
	valist2 := widget.NewSelect(vadata, func(value string) {
		//fmt.Println("Select set to", value)
	})
	//content3 := widget.NewLabel("修改数量")
	input3 := widget.NewEntry()
	input3.SetPlaceHolder("修改数量")

	input4 := widget.NewEntry()
	input4.SetPlaceHolder("")
	input4.Disable()
	input5 := widget.NewEntry()
	input5.SetPlaceHolder("")
	input5.Disable()


	button := widget.NewButton("确定", func() {
		text1 := str_curt(input1.Text)
		text2 := str_curt(input2.Text)
		_, err := strconv.Atoi(input3.Text)
		if err != nil || text1 == "" || text2 == "" {
			M_dialog("修改提示", "信息填写错误", func(mbool bool){}, myWindow)
		}else{
			ret1, ret2 := my_data_modify(text1, text2, input3.Text, ladata[lalist1.SelectedIndex()], vadata[valist1.SelectedIndex()], ladata[lalist2.SelectedIndex()], vadata[valist2.SelectedIndex()])
			input4.SetPlaceHolder(ret1)
			input5.SetPlaceHolder(ret2)
		}
		//sadata = []string{ret}
		//list.Refresh()
	})

	//input3.MinSize().Width = 50
	//fmt.Println(input3.MinSize().Width)
	//label := widget.NewLabel("0")
	lalist1.SetSelectedIndex(0)
	valist1.SetSelectedIndex(0)
	lalist2.SetSelectedIndex(0)
	valist2.SetSelectedIndex(0)
	//box1 := container.New(layout.NewVBoxLayout(),content1,input1)
	//box2 := container.New(layout.NewHBoxLayout(),input2)
	//input1.Resize(fyne.NewSize(100,input3.MinSize().Height))
	
	grid1 := container.New(layout.NewVBoxLayout(), content1, input1,layout.NewSpacer(), lalist1, valist1,layout.NewSpacer())

	grid2 := container.New(layout.NewVBoxLayout(), content2, input2,layout.NewSpacer(), lalist2, valist2,layout.NewSpacer())

	//grid3 := container.New(layout.NewVBoxLayout(), content3, input3, layout.NewSpacer(),list,lalist3,layout.NewSpacer())
	
	grid  := container.New(layout.NewHBoxLayout(), grid1, layout.NewSpacer(),grid2)

	grid4 := container.New(layout.NewVBoxLayout(), grid, input3, layout.NewSpacer(), input4, input5, layout.NewSpacer(), button)

	myWindow.SetContent(grid4)
	myWindow.SetCloseIntercept(func(){
		fmt.Println("禁止关闭窗口")
		//myWindow.Hide()
		t_data <- 6
	})
	//myWindow.Resize(fyne.NewSize(500, 300))
	//myWindow.CenterOnScreen()
	//myWindow.ShowAndRun()

	return myWindow
}


func my_data_modify(datam ...string) (string,string) {
	name1 	:= datam[0]
	name2 	:= datam[1]
	sum 	:= datam[2]
	chhanl1 := datam[3]
	chhanl2 := datam[5]
	model1 	:= datam[4]
	model2 	:= datam[6]
	
	data, err := http_data(4,name1,chhanl1,model1,name2,chhanl2,model2,sum)
	if err != nil {
		return name1  + " : " + "0", name2  + " : " + "0"
	}
    sum, err  = http_data(5, name2,chhanl2,model2)
    if err != nil {
    	return name1 + " : " + data, name2  + " : " + "0"
    }
	return name1  + " : " + data, name2  + " : " + sum
}
